package com.hb0730.boot.admin.modules.sci.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hb0730.boot.admin.base.R;
import com.hb0730.boot.admin.core.service.BaseServiceImpl;
import com.hb0730.boot.admin.data.domain.BasePage;
import com.hb0730.boot.admin.modules.sci.mapper.SciPaperConferenceMapper;
import com.hb0730.boot.admin.modules.sci.model.bo.SciExportBo;
import com.hb0730.boot.admin.modules.sci.model.entity.SciPaperConference;
import com.hb0730.boot.admin.modules.sci.model.query.SciPaperConferenceQuery;
import com.hb0730.boot.admin.modules.sci.util.ExportFileUtil;
import com.hb0730.boot.admin.security.model.UserInfo;
import com.hb0730.boot.admin.security.util.SecurityUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SciPaperConferenceService extends BaseServiceImpl<SciPaperConferenceMapper, SciPaperConference> {
    private SciMessageService sciMessageService;
    public List<SciPaperConference> findAll(String sqlTableName, List<String> selectedFields, LocalDate startTime, LocalDate endTime,String timeType) {
        return this.baseMapper.findAll(sqlTableName ,selectedFields ,startTime,endTime,timeType);
    }

    public BasePage<SciPaperConference> queryPage(SciPaperConferenceQuery query) {
        UserInfo userInfo = SecurityUtil.getCurrentUser();
        List<String> roles = userInfo.getRoles();
        List<String> permissions = userInfo.getPermissions();
        List<Integer> statusList = query.getStatus() == null ? new ArrayList<>() : new ArrayList<>(query.getStatus());
        if (!roles.contains("admin")) {
            if (permissions.contains("sci:paper:conference:query:dep_head")) {
                if (statusList.isEmpty()) {
                    statusList.add(3);
                    statusList.add(5);
                }else{
                    statusList.retainAll(Arrays.asList(3, 5));
                }
                query.setDepartment(userInfo.getDepartment());
            } else if (permissions.contains("sci:paper:conference:query:sch_head")) {
                if (statusList.isEmpty()) {
                    statusList.add(4);
                    statusList.add(5);
                }else{
                    statusList.retainAll(Arrays.asList(4, 5));
                }
            }else if (permissions.contains("sci:paper:conference:query:reviewer")) {
                statusList.retainAll(List.of(5));
            } else if (permissions.contains("sci:paper:conference:query:user")) {
                query.setUsername(userInfo.getUsername());
            } else  return new BasePage<>();
        }
        Page<SciPaperConference> page = new Page<>(query.getCurrent(), query.getSize());
        query.setStatus(statusList);
        List<SciPaperConference> vos = this.baseMapper.queryPage(page, query);
        return new BasePage<>(page.getCurrent(), page.getSize(), page.getTotal(), vos);
    }
    public R<SciPaperConference> updateById(String id, SciPaperConference sciPaperConference) {
        SciPaperConference sciPaperConference0 = getById(id);
        if (null == sciPaperConference0) {
            return R.NG("不存在");
        }
        sciPaperConference.setStatus(null);
        updateById(sciPaperConference);
        return R.OK(sciPaperConference);
    }
    @Transactional(rollbackFor = Exception.class)
    public R<SciPaperConference> saveConference(SciPaperConference sciPaperConference) {
        UserInfo userInfo = SecurityUtil.getCurrentUser();
        // 使用工厂方法构建SciPaperConference对象
        SciPaperConference conference = buildConference(sciPaperConference, userInfo);
        super.save(conference);

        return R.OK(conference);
    }
    public R<String> deleteByIds(List<String> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return R.NG("参数不能为空");
        }
        removeByIds(ids);
        sciMessageService.deleteBySciIds(ids);
        return R.OK();
    }
    // 构建SciPaperConference对象，避免直接修改原始对象
    private SciPaperConference buildConference(SciPaperConference sciPaperConference, UserInfo userInfo) {
        sciPaperConference.setDepartment(userInfo.getDepartment());
        sciPaperConference.setUsername(userInfo.getUsername());
        sciPaperConference.setNickname(userInfo.getNickname());
        sciPaperConference.setStatus(3); // 默认状态为1，必要时可以改为常量
        return sciPaperConference;
    }

    public void exportData(HttpServletResponse response, SciExportBo sciExportBo) {
    // 获取 query 字段，它是一个 Map<String, String>
    Map<String, String> queryMap = sciExportBo.getQuery();
    // 创建 SciPaperConferenceQuery 对象
    SciPaperConferenceQuery query = new SciPaperConferenceQuery();
    ModelMapper modelMapper = new ModelMapper();
    // 将 queryMap 中的字段自动映射到 SciPaperConferenceQuery 对象
    modelMapper.map(queryMap, query);
    List<SciPaperConference> vos = this.baseMapper.queryPage(null,query);
         try {
            // 调用工具类导出数据
            ExportFileUtil.exportData(response,sciExportBo.getValues(), vos);
        } catch (IOException e) {
            // 处理异常
            e.printStackTrace();
        }
    }
}
